'''
Created on Aug 17, 2010

@author: Tom Smith
'''
import Robot
import time
from Timing import Timing
from TwainControl import TwainBase
import os
#import win32api
import Tkinter
import twain
import Database.Database as DB
import Database.DbFunctions as DBF
import cv

conn = DB.databaseGetConn()
#import cv

#Variables
location_id = '4'
user_id = '1'
scanner_id = '1'


F=Tkinter.Frame()
window = F.winfo_id()
#cv.NamedWindow('Camera', True)

 
artifact_id = None
name = None
robot = Robot.Robot('COM3')
time.sleep(5)
robot.getData()
#Start scanning sequence


#Step 1
robot.moveY('f')
robot.zeroTheMachine()

#2
robot.openScanner()

#3
robot.moveY('f')


#Repeat this later

#4

while(1):
    
    T = Timing()
    
    T.startTimer()
    robot.gotoBin1()
    print "Going to bin 1 level"
    #robot.gotoBin1Level()
    print "Went to bin 1 level"
    T.stopTimer()
    
    #5
    T.startTimer()
    robot.lowerAndLiftInternal()
    T.stopTimer()
    
    #6
    T.startTimer()
    robot.gotoScanner()
    #robot.setNextPosition((15000, 7000, 7000))
    #robot.gotoNextPosition()                
    T.stopTimer()
    
    #7
    T.startTimer()
    robot.toggleVacuum()
    time.sleep(5)
    T.stopTimer()
    
    #8
    T.startTimer()
    robot.gotoRetreat()
    T.stopTimer()
    
    #8.5
    if artifact_id:
        T.startTimer()
        cap = cv.CaptureFromCAM(0)
        cv.SetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT, 1280)
        cv.SetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH, 720)
        image = cv.QueryFrame(cap)
        #cv.ShowImage('Camera', image)
        name = name + "a"
        image_name = "%s.jpg" % name
        raw_path = "G:\\raw_scans\\%s" % image_name
        conn.ping(True)
        cursor = DB.getCursorFromConn(conn)
        cv.SaveImage(raw_path, image)
        raw_path = "raw_scans\\\%s" % image_name
        DBF.addImageToArtifact(conn, cursor, artifact_id, name, raw_path, scanner_id, '2', user_id)
        conn.commit()
        cursor.close()
        print "Captured image of back"
        T.stopTimer()
    
    #9
    T.startTimer()
    robot.closeScanner()
    T.stopTimer()
    
    #10
    T.startTimer()
    scanner = TwainBase()
    scanner.OpenScanner(window, "CanoScan LiDE 70")
    scanner.SD.SetCapability(twain.ICAP_YRESOLUTION, twain.TWTY_FIX32, 600.0) 
    scanner.SD.SetCapability(twain.ICAP_XRESOLUTION, twain.TWTY_FIX32, 600.0)
    print scanner.SD.GetCapability(twain.ICAP_CONTRAST)
    print scanner.SD.GetCapability(twain.ICAP_PIXELTYPE)
    scanner.SD.SetCapability(twain.ICAP_PIXELTYPE, twain.TWTY_UINT16, twain.TWPT_GRAY)
    scanner.AcquireByFile()
    watch = ''
    while(watch == ''):
        watch = scanner.PollForImage()
        time.sleep(5)
   # scanner.Terminate()
    conn.ping(True)
    cursor = DB.getCursorFromConn(conn)
    next_i = DBF.getNextNumForLocation(cursor, location_id)
    base_name = DBF.getBaseNameForLocation(cursor, location_id)
    name = "%s_%s" % (base_name, next_i)
    image_name = "%s.tiff" % name
    raw_path = "G:\\raw_scans\\%s" % image_name
    print raw_path
    os.rename('TWAIN.tmp', raw_path)
    raw_path = "raw_scans\\\%s" % image_name
    artifact_id = DBF.makeNewArtifact(conn, cursor, location_id, next_i, base_name, user_id, 0)
    DBF.addImageToArtifact(conn, cursor, artifact_id, name, raw_path, scanner_id, '1', user_id)
    conn.commit()
    cursor.close()
    T.stopTimer()
    
    #11
    T.startTimer()
    robot.openScanner()
    T.stopTimer()
    
    #12
    T.startTimer()
    robot.moveY('f')
    robot.gotoScanner()
    T.stopTimer()
    
    #13
    T.startTimer()
    #robot.gotoScannerLevel()
    robot.lowerAndLiftInternal()
    T.stopTimer()
    
    #14
    T.startTimer()
    print "Going to bin 2"
    robot.gotoBin2()
    T.stopTimer()
    print "Arrived at bin 2"
    
    #15
    T.startTimer()
    print "Dropping the photo"
    robot.toggleVacuum()
    time.sleep(5)
    T.stopTimer()
    print "Starting again"
    
    

    
    #total_time = T.seconds()
   # conn.ping(True)
   # cursor = DB.getCursorFromConn(conn)
  #  query = "INSERT INTO timing (dt, overall_time) VALUES (now(), '%s')" % total_time
   # DB.executeCommand(query, cursor)


if __name__ == '__main__':
    pass